package com.qzsoft.zhjd.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
//    private static String parsePatterns = "yyyy-MM-dd HH:mm:ss";

    private static final long ONE_MINUTE = 60;
    private static final long ONE_HOUR = 3600;
    private static final long ONE_DAY = 86400;
    private static final long ONE_MONTH = 2592000;
    private static final long ONE_YEAR = 31104000;
    /**
     * 功能描述:返回当前时间
     * @param patterns 时间格式
     * @return : java.util.Date
     * @author : liugf
     * @date : 2021/4/14 18:48
     */
    public static Date getDate(String patterns){
        SimpleDateFormat sdf = new SimpleDateFormat(patterns);
        String s = sdf.format(new Date());
        try{
            Date date =  sdf.parse(s);
            return date;
        }catch (ParseException ex){
            return null;
        }
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str)
    {
        if (str == null)
        {
            return null;
        }
            return parseDate(str.toString());
    }

    public static final String parseDateToStr(final String format, final Date date)
    {
        return new SimpleDateFormat(format).format(date);
    }

    /**
     * 获取上月最后一天
     */
    public static String getPreMonthLastDay(){
        return "";
    }
    /**
     * 获取本月最后一天
     */
    public static String getMonthEnd(Date date) throws Exception
    {
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        //获取当前月最后一天
        Calendar currentDate = Calendar.getInstance();
        currentDate.setTime(date);
        currentDate.set(Calendar.DAY_OF_MONTH, currentDate.getActualMaximum(Calendar.DAY_OF_MONTH));
        return format.format(currentDate.getTime());
    }

    /**
     * 获取本月第一天
     */
    public static String getMonthFirst(Date date) throws Exception
    {
        String pattern = "yyyy-MM-dd";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH,1);
        calendar.add(Calendar.MONTH,0);
        return format.format(calendar.getTime());
    }

    /**
     * 获取当年的第一天
     * @param year
     * @return
     */
    public static String getCurrYearFirst(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar currCal=Calendar.getInstance();
        currCal.setTime(date);
        int currentYear = currCal.get(Calendar.YEAR);
        return sdf.format(getYearFirst(currentYear));
    }

    /**
     * 获取某年第一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }

    /**
     * 计算两个时间差
     */
    public static String getDatePoor(String endDate, String nowDate)
    {
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            //LocalDateTime转换为Date
            Date dd = Date.from(LocalDateTime.parse(endDate,df).atZone(ZoneId.systemDefault()).toInstant());
            Date dt = Date.from(LocalDateTime.parse(nowDate,df).atZone(ZoneId.systemDefault()).toInstant());

            long time = dd.getTime() / 1000;
            long now = dt.getTime() / 1000;
            long ago = now - time;
            if (ago <= ONE_HOUR)
                return ago / ONE_MINUTE + "分钟"+(ago % ONE_MINUTE )+"秒";
            else if (ago <= ONE_DAY)
                return ago / ONE_HOUR + "小时" + (ago % ONE_HOUR / ONE_MINUTE)
                        + "分钟";
            else if (ago <= ONE_DAY * 2)
                return "1天";
            else if (ago <= ONE_DAY * 3)
                return "2天";
            else if (ago <= ONE_MONTH) {
                long day = ago / ONE_DAY;
                return day + "天" ;
            } else if (ago <= ONE_YEAR) {
                long month = ago / ONE_MONTH;
                long day = ago % ONE_MONTH / ONE_DAY;
                return month + "个月" + day + "天";
            } else {
                long year = ago / ONE_YEAR;
                return year + "年" ;
            }
    }
}
